<?php
//require_once SITE_ROOT_PATH."/_lib/base/curl_multirequest.php";
//require_once SITE_ROOT_PATH.'/_lib/classes/Supplier/low/class.QueryBuilder.php';

class hotel_supplier_low {
   
    protected
        $code, 
        $name,
        $account,
        $reference,
        $method,
        $services,
        $search_params_map,
        $booking_params_map;

    public function __construct() {

        $this->code = "low";
        $this->name = "LowCostGroup";
        $this->account = array('username'=>'aggtestie','password'=>'test1e');
        $this->reference = "IRELAND";
        $this->method = "POST";     

    }

    public function do_search($params) {

    }

    public function do_book($params) {

    }

    public function do_cancel_booking($params) {

    }
    
    
    public function get_hotel_description($hotel_id) {
        
        if(isset($hotel_id)){

            $sql = "SELECT * FROM supplier_properties_low
            INNER JOIN supplier_locations_low ON supplier_locations_low.resort_id = supplier_properties_low.GeographyLevel3ID
            WHERE PropertyID = '".$hotel_id."'
            LIMIT 1 "; 

            return apply_query($sql);

        }

    }
    
    

  public function get_hotel_details($params) {
    $params = $this->map_details_params($params);
    $uri_params = array();
    // authentification info
    $uri_params[] = $this->auth["username_name"]."=".$this->auth["username_value"];
    $uri_params[] = $this->auth["password_name"]."=".$this->auth["password_value"];
    foreach($params as $param=>$value) {
      $uri_params[] = "$param=$value";
    }    
    $url = $this->services["details"]["url"]."?".implode("&", $uri_params);
        
    $xml = file_get_contents($url);
    
    return $xml;    
  }

  private function log_booking_result($message) {
    $filename = "booking-".date("YmdHis");
    file_put_contents("logs/$filename.xml", $message);    
  }
  private function log_cancel_booking_result($message) {
    $filename = "cancel_booking-".date("YmdHis");
    file_put_contents("logs/$filename.xml", $message);    
  }
  
  public function get_id() {
    return $this->code;
  }  
  public function get_name() {
    return $this->name;
  }
  public function get_xslt_path($type) {
    return $this->services[$type]["response_xslt"];
  }

  protected function load_params_map($map) {
    $params_map = array();
    $fh = fopen($this->services[$map]["paramsmapfile"], "r");
    if(!$fh) throw new Exception("Can't load $map params map file for supplier $this->code");
    while($row = fgetcsv($fh, 1024)) {
      $param_props = array();
      $props_string = explode(";", $row[1]);
      foreach($props_string as $prop) {
        $ret = explode("=", $prop);
        $param_props[$ret[0]] = $ret[1];
      }
      $params_map[$row[0]] = $param_props;
    }
    return $params_map;
  }
  
  protected function load_search_map() {
    if(!$this->search_params_map) {
      $this->search_params_map = $this->load_params_map('search');
    }
  }
  
  protected function load_details_map() {
    if(!$this->details_params_map) {
      $this->details_params_map = $this->load_params_map('details');
    }    
  }

  protected function map_params($params, $map) {
    $result = array();
    foreach($map as $param=>$props) {
      if(isset($props["fname"])) {
        if(isset($params[$props["fname"]])) {
          // if we have value to read from user form - read and format it
          $value = $params[$props["fname"]];
          if(isset($props["type"])) {
            switch($props["type"]) {
              case "date":
                // format date field according to format
                if(isset($props["format"])) {
                  $value = date($props["format"], $value);
                }
                else {
                  $value = date(SUPPLIER_DEFAULT_DATE_FORMAT, $value);
                }
              break;
            }
          }        
          $result[$param] = $value;
        }
        else {
          // we have no value from user form, try to find default value          
          if(isset($props["default"])) {
            $result[$param] = $props["default"];
          }
          // if no default value - skip parameter
        }
      }
      else {
        // if no fname parameter set - try to use default value for current parapeter
        if(isset($props["default"])) {
          $result[$param] = $props["default"];
        }
      }
    }
    return $result;
  }

  protected function map_search_params($form_params) {
    $this->load_search_map();
    return $this->map_params($form_params, $this->search_params_map);
  }
  
  protected function load_booking_map() {
    if(!$this->booking_params_map) {
      $this->booking_params_map = $this->load_params_map('booking');
    }
  }
  
    protected function load_cancel_booking_map() {
        if (!$this->cancel_booking_params_map) {
            $this->cancel_booking_params_map = $this->load_params_map('cancel_booking');
        }
        return $this->cancel_booking_params_map;
    }
  
  protected function map_booking_params($form_params) {
    $this->load_booking_map();
    return $this->map_params($form_params, $this->booking_params_map);    
  }
  
    protected function map_cancel_booking_params($params) {
        return $this->map_params($params, $this->load_cancel_booking_map());    
    }
  
  protected function map_details_params($params) {
    $this->load_details_map();
    return $this->map_params($params, $this->details_params_map);
  }
  
  
 

      function get_resorts_from_table($tree_id){
        /*
         * Return a list of resorts that are mapped to a LateDeals tree.tree_id
         */
        
        
           if(isset($tree_id)){
           
               $tree_id = in($tree_id);

            
                $sql = "SELECT * 
                        FROM supplier_locations_low
                        WHERE tree_id = '".$tree_id."' ";   
                return apply_query($sql);
            
            }
               
               
       }
  
  
  
  
  
}
